package com.zfzs.post.modules.agent.service.impl.mybatis;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zfzs.post.model.dto.QueryAgentPageListDTO;
import com.zfzs.post.model.dto.QueryFindAgentInfoPageDTO;
import com.zfzs.post.model.dto.QueryTeamRankPageDTO;
import com.zfzs.post.model.vo.QueryAddAgentListVO;
import com.zfzs.post.model.vo.QueryAgentPageListVO;
import com.zfzs.post.model.vo.QueryTeamRankPageVO;
import com.zfzs.post.modules.agent.domain.Agent;
import com.zfzs.post.modules.agent.service.mapper.mybatis.IAgentMapper;
import com.zfzs.post.modules.agent.service.mybatis.IAgentService;
import com.zfzs.post.page.JpaPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 代理商
 *
 * @author: ck
 * @create: 2019/10/12/11:41
 **/
@Service("iAgentService")
public class IAgnetServiceImpl extends ServiceImpl<IAgentMapper, Agent> implements IAgentService {

    @Autowired
    private IAgentMapper agentMapper;

    @Override
    public JpaPageHelper findAgentPageList(QueryAgentPageListDTO dto) {
        int pageNum = dto.getPageNum();
        int pageSize = dto.getPageSize();
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);//借助计算起始位置
        // 计算数据总条数
        int total = agentMapper.findAgentPageCount(dto);
        List<Map<String, Object>> list = agentMapper.findAgentPageList(dto, pageRequest.getOffset(), pageRequest.getPageSize());
        JpaPageHelper page = new JpaPageHelper<>(list, pageNum, pageSize, total);
        // 转换分页数据
        page.setResult(list);
        return page;
    }

    @Override
    public List<Map<String, Object>> findAgentInfoPageList(QueryFindAgentInfoPageDTO dto, long pageNum, int pageSize) {
        return agentMapper.findAgentInfoPageList(dto, pageNum, pageSize);
    }

    @Override
    public int findAgentInfoPageCount(QueryFindAgentInfoPageDTO dto) {
        return agentMapper.findAgentInfoPageCount(dto);
    }

    @Override
    public int findByPhoneAndChannel(String phone, Long channelId) {
        return agentMapper.findByPhoneAndChannel(phone, channelId);
    }

    @Override
    public JpaPageHelper getTeamRankPage(QueryTeamRankPageDTO dto) {
        int pageNum = dto.getPageNum();
        int pageSize = dto.getPageSize();
        //借助计算起始位置
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        // 计算数据总条数
        int total = agentMapper.getTeamRankPageCount(dto);
        List<QueryTeamRankPageVO> teamList = new ArrayList<>();
        List<Map<String, Object>> list = agentMapper.getTeamRankPage(dto, pageRequest.getOffset(), pageRequest.getPageSize());
        JpaPageHelper page = new JpaPageHelper<>(list, pageNum, pageSize, total);
        for (int i = 0; i < list.size(); i++) {
            QueryTeamRankPageVO team = JSON.parseObject(JSON.toJSONString(list.get(i)), QueryTeamRankPageVO.class);
            //总排名
            team.setAllRank(i+1);
            //团队排名

            teamList.add(team);

        }
        // 转换分页数据
        page.setResult(teamList);
        return page;
    }
}
